package Server.tool;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;

import java.math.BigDecimal;

public class AliPayE {
    public static String pay(BigDecimal money,String name,String mateid){
        // 该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天。
        String timeout_express = "15d";
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl, AliPayConfig.app_id, AliPayConfig.merchant_private_key, "json", AliPayConfig.charset, AliPayConfig.alipay_public_key, AliPayConfig.sign_type);
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl(AliPayConfig.notify_url);
        request.setBizContent("{"+
                "\"out_trade_no\":\""+mateid+"\","+
                "\"total_amount\":"+money+","+
                "\"timeout_express\":\""+timeout_express+"\","+
                "\"subject\":\""+name+"\"}");
        AlipayTradePrecreateResponse res = null;
        try {
            res = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            System.out.println(e.getMessage());
        }
        String bodys = res.getBody();
        JSONObject jsonObject = JSONObject.parseObject(bodys);
        String text = jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code");
        return text;
    }
}
